package com.dq.utils;


import com.dq.domain.category.Category;
import com.dq.domain.sys.macro.SysMacro;
import com.dq.domain.sys.menu.SysMenu;
import com.dq.domain.position.OrgPosition;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.service.ServiceProjectCategory;
import com.google.common.collect.Lists;

import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class TreeBuilder {


    /**
     * 岗位tree
     */
    public static List<OrgPosition.Vo> orgPositionBuildTree(Set<OrgPosition.Vo> allNodes) {
        // 根节点
        Set<OrgPosition.Vo> root = new HashSet<>();
        allNodes.forEach(node -> {
            if (node.getParentId() == null) {
                root.add(node);
            }
        });
        root.forEach(node -> {
            findChildOrgPosition(node, allNodes);
        });
        //对根节点排序
        List<OrgPosition.Vo> sortedList = root.stream().sorted(Comparator.comparing(OrgPosition.Vo::getSortNo)).collect
                (Collectors.toList());
        //先清空，在添加
        root.clear();
        root.addAll(sortedList);
        return sortedList;
    }
    /**
     * 递归查找子节点
     */
    private static OrgPosition.Vo findChildOrgPosition(OrgPosition.Vo treeNode, Set<OrgPosition.Vo> treeNodes) {
        for (OrgPosition.Vo it : treeNodes) {
            if (it.getParentId() != null && treeNode.getId().toString().equals(it.getParentId().toString())) {
                treeNode.getChilds().add(findChildOrgPosition(it, treeNodes));
            }
        }
        //对子节点排序
        List<OrgPosition.Vo> childrenSorted = treeNode.getChilds().stream().sorted(Comparator.comparing
                (OrgPosition.Vo::getSortNo)).collect(Collectors.toList());
        //先清空，在添加
        treeNode.getChilds().clear();
        treeNode.getChilds().addAll(childrenSorted);
        return treeNode;
    }

    /**
     * 服务项目类别
     */
    public static List<ServiceProjectCategory.Vo> serviceProjectCategoryBuildTree(Set<ServiceProjectCategory.Vo> allNodes) {
        // 根节点
        Set<ServiceProjectCategory.Vo> root = new HashSet<>();
        allNodes.forEach(node -> {
            if (node.getParentId() == null) {
                root.add(node);
            }
        });
        root.forEach(node -> {
            findChildServiceProjectCategory(node, allNodes);
        });
        //对根节点排序
        List<ServiceProjectCategory.Vo> sortedList = root.stream().sorted(Comparator.comparing(ServiceProjectCategory.Vo::getSortNo)).collect
                (Collectors.toList());
        //先清空，在添加
        root.clear();
        root.addAll(sortedList);
        return sortedList;
    }
    private static ServiceProjectCategory.Vo findChildServiceProjectCategory(ServiceProjectCategory.Vo treeNode, Set<ServiceProjectCategory.Vo> treeNodes) {
        for (ServiceProjectCategory.Vo it : treeNodes) {
            if (it.getParentId() != null && treeNode.getId().toString().equals(it.getParentId().toString())) {
                treeNode.getChilds().add(findChildServiceProjectCategory(it, treeNodes));
            }
        }
        //对子节点排序
        List<ServiceProjectCategory.Vo> childrenSorted = treeNode.getChilds().stream().sorted(Comparator.comparing
                (ServiceProjectCategory.Vo::getSortNo)).collect(Collectors.toList());
        //先清空，在添加
        treeNode.getChilds().clear();
        treeNode.getChilds().addAll(childrenSorted);
        return treeNode;
    }

    /**
     * 组织部门属性
     */
    public static List<SysSection.Vo> buildTreeSysSection(Set<SysSection.Vo> allNodes) {
        // 根节点
        Set<SysSection.Vo> root = new HashSet<>();
        allNodes.forEach(node -> {
            if (node.getParentId() != null && node.getParentId().toString().equals("0")) {
                root.add(node);
            }
        });
        root.forEach(node -> {
            findChildrenSysSection(node, allNodes);
        });

        return root.stream().sorted(Comparator.comparing(SysSection.Vo::getSort)).collect(Collectors.toList());
    }
    /**
     * 递归查找子节点
     */
    private static SysSection.Vo findChildrenSysSection(SysSection.Vo treeNode, Set<SysSection.Vo> treeNodes) {
        for (SysSection.Vo it : treeNodes) {
            if (it.getParentId() != null && treeNode.getId().toString().equals(it.getParentId().toString())) {
                treeNode.getChildren().add(findChildrenSysSection(it, treeNodes));
            }
        }
        return treeNode;
    }




    public static List<SysMenu.Vo> buildTree(Set<SysMenu.Vo> allNodes) {
        // 根节点
        Set<SysMenu.Vo> root = new HashSet<>();
        allNodes.forEach(node -> {
            if (node.getParentId() != null && node.getParentId() == 1) {
                root.add(node);
            }
        });
        root.forEach(node -> {
            findChildren(node, allNodes);
        });

        return root.stream().sorted(Comparator.comparing(SysMenu.Vo::getOrderNum)).collect(Collectors.toList());
    }
    /**
     * 递归查找子节点
     */
    private static SysMenu.Vo findChildren(SysMenu.Vo treeNode, Set<SysMenu.Vo> treeNodes) {
        for (SysMenu.Vo it : treeNodes) {
            if (it.getParentId() != null && treeNode.getId() == it.getParentId()) {
                treeNode.getChilds().add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }


    public static List<Category.Vo> buildCategoryTree(List<Category.Vo> allNodes) {
        // 根节点
        List<Category.Vo> root = Lists.newArrayList();
        for (Category.Vo node : allNodes) {
            if (node.getParentId() != null && node.getParentId() == 1) {
                root.add(node);
            }
        }
        root.forEach(node -> {
            findCategoryChildren(node, allNodes);
        });

        //对根节点排序
        List<Category.Vo> sortedList = root.stream().sorted(Comparator.comparing(Category.Vo::getSortNo)).collect
                (Collectors.toList());
        //先清空，在添加
        root.clear();
        root.addAll(sortedList);
        return root;
    }
    /**
     * 递归查找子节点
     */
    private static Category.Vo findCategoryChildren(Category.Vo treeNode, List<Category.Vo> treeNodes) {
        for (Category.Vo it : treeNodes) {
            if (it.getParentId() != null && treeNode.getId() == it.getParentId()) {
                treeNode.getChilds().add(findCategoryChildren(it, treeNodes));
            }
        }
        //对子节点排序
        List<Category.Vo> childrenSorted = treeNode.getChilds().stream().sorted(Comparator.comparing
                (Category.Vo::getSortNo)).collect(Collectors.toList());
        //先清空，在添加
        treeNode.getChilds().clear();
        treeNode.getChilds().addAll(childrenSorted);
        if(treeNode.getChilds().size() <= 0) {
            treeNode.setChilds(null);
        }
        return treeNode;
    }


    public static List<SysMacro.Vo> buildMacroTree(List<SysMacro.Vo> allNodes) {
        // 根节点
        Set<SysMacro.Vo> root = new HashSet<>();
        allNodes.forEach(node -> {
            if (node.getParentId() != null && node.getParentId() == 1) {
                root.add(node);
            }
        });
        root.forEach(node -> {
            findMacroChildren(node, allNodes);
        });

        List<SysMacro.Vo> sortedList = root.stream().sorted(Comparator.comparing(SysMacro.Vo::getOrderNum)).collect
                (Collectors.toList());
        return sortedList;
    }
    /**
     * 递归查找子节点
     */
    private static SysMacro.Vo findMacroChildren(SysMacro.Vo treeNode, List<SysMacro.Vo> treeNodes) {
        for (SysMacro.Vo it : treeNodes) {
            if (it.getParentId() != null && treeNode.getId() == it.getParentId()) {
                treeNode.getChilds().add(findMacroChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    /**
     * 分类tree
     */
    public static List<Category.Vo> categoryBuildTree(Set<Category.Vo> allNodes) {
        // 根节点
        Set<Category.Vo> root = new HashSet<>();
        allNodes.forEach(node -> {
            if (node.getParentId() == 1) {
                root.add(node);
            }
        });
        root.forEach(node -> {
            findChildCategory(node, allNodes);
        });
        //对根节点排序
        List<Category.Vo> sortedList = root.stream().sorted(Comparator.comparing(Category.Vo::getSortNo)).collect
                (Collectors.toList());
        //先清空，在添加
        root.clear();
        root.addAll(sortedList);
        return sortedList;
    }

    /**
     * 递归查找子节点
     */
    private static Category.Vo findChildCategory(Category.Vo treeNode, Set<Category.Vo> treeNodes) {
        for (Category.Vo it : treeNodes) {
            if (it.getParentId() != null && treeNode.getId().toString().equals(it.getParentId().toString())) {
                treeNode.getChilds().add(findChildCategory(it, treeNodes));
            }
        }
        //对子节点排序
        List<Category.Vo> childrenSorted = treeNode.getChilds().stream().sorted(Comparator.comparing
                (Category.Vo::getSortNo)).collect(Collectors.toList());
        //先清空，在添加
        treeNode.getChilds().clear();
        treeNode.getChilds().addAll(childrenSorted);
        return treeNode;
    }
}